<template>
  <div class="view_page userList">
    <el-card class="box-card">
      <!--卡片头部-->
      <div slot="header" class="clearfix">
        <!--左侧-->
        <div class="left">
          <div class="menu-title">
            模型文件管理
          </div>
        </div>
        <!--右侧-->
        <div class="right">
          <el-button type="primary" size="small" @click="navtoAdd('add')"
            >新增
          </el-button>
        </div>
      </div>
      <!-- 卡片内容 -->
      <div>
        <!--筛选-->
        <div class="query-operate">
          <el-form
            class="demo-form-inline indoor-scene-buzhou-list-form"
            :inline="true"
            :model="TESTFROMDATA"
            @submit.native.prevent
          >
            <el-form-item
              label="模型文件名称"
              label-width="120px"
              style="margin-top: 5px; margin-bottom: 5px;"
            >
              <el-input
                clearable
                @clear="searchList"
                v-model="TESTFROMDATA.name"
                @keyup.native.enter="searchList()"
              ></el-input>
            </el-form-item>
            <el-form-item
              style="margin-top: 5px; margin-bottom: 5px;margin-left: 10px;"
            >
              <el-button type="primary" @click="searchList()">查询</el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-table :data="datalist" style="width: 100%">
          <el-table-column
            prop="id"
            label="模型ID"
            align="center"
            min-width="200"
          >
          </el-table-column>
          <el-table-column
            prop="name"
            label="模型文件名称"
            align="center"
            min-width="200"
          >
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            min-width="300"
            align="center"
          >
            <template slot-scope="scope">
              <div style="display: flex;align-items: center;flex-wrap: wrap; ">
                <el-button
                  type="primary"
                  size="small"
                  @click="toModelFilePronList(scope.row.id)"
                  >版本列表
                </el-button>
                <el-button
                  type="primary"
                  size="small"
                  @click="navtoAdd('update', scope.row.id)"
                  >修改</el-button
                >
                <el-button type="danger" size="small" @click="del(scope.row)"
                  >删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <pagination
          :total="total"
          :currentPage="pageNum"
          :pageSize="size"
          position="right"
          @search="searchPagination"
        >
        </pagination>
      </div>
    </el-card>

    <!--修改弹框-->
    <el-dialog
      :title="addtype == 'add' ? '新增模型文件' : '修改模型文件'"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form ref="form" label-width="120px">
        <el-form-item label="模型文件名称">
          <el-input
            v-model="liveData.name"
            placeholder="请输入模型文件名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import qs from "qs";
import { Message } from "element-ui";
import pagination from "@/components/common/pagination/pagination";
import devConfig from "*/dev.config";

export default {
  components: {
    pagination
  },
  watch: {},
  data() {
    return {
      // 筛选内容
      TESTFROMDATA: {
        name: ""
      },

      //富文本文档类型
      liveData: {
        name: ""
      },
      dialogVisible: false, //添加模型文件名称
      addtype: "", //类型 新增-修改

      // 列表的数据
      datalist: [
        {
          "id": 1,
          "name": "马河水库"
        },
        {
          "id": 2,
          "name": "户主水库"
        },
      ],
      pageNum: 1,
      page: 1,
      size: 10,
      total: 0
    };
  },
  created() {
    this.page = localStorage.getItem("page");
    this.getListPage();
  },
  mounted() {},
  methods: {
    //查询列表
    getListPage() {
      var _this = this;
      var datas = {
        name: _this.TESTFROMDATA.name,
        page: _this.page,
        size: _this.size
      };
      _this.$apis.u_model_list(datas).then(res => {
        if (res.result == 0) {
          _this.datalist = res.data;
          _this.total = res.rows;
          _this.pageNum = res.page;
          localStorage.setItem("page", 1);
        }
      });
    },

    //模糊查询
    searchList() {
      this.page = 1;
      this.datalist = [];
      this.getListPage();
    },
    //分页
    searchPagination(page) {
      this.page = page;
      localStorage.setItem("page", page);
      this.getListPage();
    },

    initData() {
      this.liveData = {
        name: ""
      };
    },
    //跳转新增
    navtoAdd(type, item) {
      var _this = this;
      _this.initData();
      if (type == "add") {
        _this.addtype = "add";
        _this.dialogVisible = true;
      } else {
        _this.itemID = item;
        _this.addtype = "updata";
        var datas = {
          id: item
        };
        _this.$apis.u_model_query(datas).then(res => {
          if (res.result == 0) {
            _this.liveData = res.data;
            _this.dialogVisible = true;
          }
        });
      }
    },
    save() {
      var _this = this;
      var datas = _this.liveData;
      if (datas.name == "") {
        Message.warning("请填写模型文件名称");
        return;
      }
      // console.log("弹窗====", datas);

      if (_this.addtype == "add") {
        _this.$apis.u_model_add(datas).then(res => {
          if (res.result == 0) {
            Message.success("新增成功");
            this.dialogVisible = false;
            this.getListPage();
          } else {
            this.dialogVisible = false;
            this.getListPage();
          }
        });
      } else {
        datas.id = _this.itemID;
        _this.$apis.u_model_update(datas).then(res => {
          if (res.result == 0) {
            Message.success("修改成功");
            this.dialogVisible = false;
            this.getListPage();
          } else {
            this.dialogVisible = false;
            this.getListPage();
          }
        });
      }
    },
    //取消
    cancel() {
      this.dialogVisible = false;
    },
    // 去详细二级列表
    toModelFilePronList(item) {
      var _this = this;
      localStorage.setItem("page", this.page);

      _this.$router.push({
        path: "/admin/modelFileManage/modelFilePronList",
        query: {
          data: item
        }
      });

      // var datas = {
      //   id: item
      // };
      // _this.$apis.u_model_file_list(datas).then(res => {
      //   if (res.result == 0) {
      //     _this.$router.push({
      //       path: "/admin/modelFileManage/modelFilePronList",
      //       query: {
      //         type: type,
      //         data: JSON.stringify(res.data)
      //       }
      //     });
      //   }
      // });
    },
    //删除
    del(item) {
      var _this = this;
      var datas = {
        id: item.id
      };
      _this
        .$confirm("确定要删除【" + item.name + "】吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => {
          _this.$apis.u_model_del(datas).then(res => {
            if (res.result == 0) {
              Message.success("删除成功");
              if (this.page > 1 && this.datalist.length <= 1) {
                this.page = this.page - 1;
              }
              _this.getListPage();
            }
          });
        })
        .catch(() => {});
    }
  }
};
</script>

<style>
.el-table .cell {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal !important;
}
</style>
